package com.hall.controller;

import com.hall.pojo.SysUser;
import com.hall.pojo.Userschedule;
import com.hall.service.ScheduleService;
import com.hall.service.UserService;
import com.hall.utils.TimeThisWeekend;
import com.hall.utils.UserSession;
import com.mchange.v1.util.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日程表
 */
@Controller
@RequestMapping("/schedule")
public class ScheduleController {
    private SysUser loginSysUser;
    @Autowired
    private ScheduleService scheduleService;
    @Autowired
    private UserService userService;

    /*日程页面*/
    @RequestMapping("/show")
    public String scheduleShow(HttpSession session, HttpServletRequest request, Model model, @RequestParam("index") String index) {
        loginSysUser = userService.getUser(UserSession.getSysUser(request).getId());
        List<Integer> time = new ArrayList<>();
        for (int i = 0; i <= 9; i++) {
            time.add(i);
        }
        model.addAttribute("sysUser", loginSysUser);
        model.addAttribute("time", time);
        model.addAttribute("loginUser", userService.getUser(loginSysUser.getId()));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        Date date = null;
        try {
            date = df.parse(df.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String thisMonday = TimeThisWeekend.getWeekByDate(date);// 获取当前系统时间对应的周一日期,作为日程表查询的起始时间
        String thisFiveday = TimeThisWeekend.getNextFiveWeek(date);  //获取当前周五日期
        String thisSunday = TimeThisWeekend.getNextSevenWeek(date); //获取当前周末日期
        List<String> days = TimeThisWeekend.getWeekDay(); //本周周1-7日期
        List<String> sunday = TimeThisWeekend.getNextWeekDay(thisSunday);//下周周1-7日期
        days.addAll(sunday);
        String nextMonday = sunday.get(0); //获取下周周一日期
        String nextFiveday = sunday.get(6); //获取下周周末日期
        //默认给本周下周的日程设置默认值,如果本来有则不设置
        for (String ss : days) {
            Userschedule twoweekend = scheduleService.findUserSchedule(new Userschedule().setUserid(loginSysUser.getId()).setScheduledate(ss)); //遍历查找两周日程
            if (twoweekend == null) {
                Userschedule schedule8 = new Userschedule()
                        .setUserid(loginSysUser.getId())
                        .setUsername(loginSysUser.getName())
                        .setScheduledate(ss)
                        .setTimeconfig("0000000000");
                int i = scheduleService.addUserSchedule(schedule8);
            }
        }
        //本周页面日程数据
        if ("this".equals(index)) {
            List<Userschedule> allSchedule = scheduleService.findAllSchedule(loginSysUser.getId(), thisMonday, thisSunday); //查询用户当前的所有日程
            model.addAttribute("allSchedule", allSchedule);
        }
        //下周页面数据
        else if ("down".equals(index)) {
            List<Userschedule> allSchedule = scheduleService.findAllSchedule(loginSysUser.getId(), nextMonday, nextFiveday); //查询用户下一周的所有日程(后端查询实现更为方便)
            model.addAttribute("allSchedule", allSchedule);
        }
        return "user_schedule_set";
    }

    /*
     * 添加日程*/
    @ResponseBody
    @RequestMapping("/set")
    public String scheduleSet(Userschedule userschedule) {
        userschedule.setUserid(loginSysUser.getId()); //登录用户信息
        Userschedule userschedule1 = scheduleService.findUserSchedule(userschedule);//查找是否存在要添加的日程
        StringBuilder ssb; //实例化stringbuilder对象存放timeconfig
        if (userschedule1 != null) {
            ssb = new StringBuilder(userschedule1.getTimeconfig());
            ssb.replace(userschedule.getRowid(), userschedule.getRowid() + 1, userschedule.getGridvalue().toString());//修改timeconfig对应前端选择的行数的状态(1,2)
            String nextThisDay = TimeThisWeekend.getNextWeek(TimeThisWeekend.ChangeTime(userschedule1.getScheduledate())); //点击那天下周对应的星期
            System.out.println("点击那天是" + userschedule1.getScheduledate());
            System.out.println("对应下周是" + nextThisDay);
            //更新日程
            int j = scheduleService.updateUserSchedule(new Userschedule().setUserid(userschedule1.getUserid()).setTimeconfig(ssb.toString()).setScheduledate(userschedule1.getScheduledate()));//更新原有该天的日程
            int e = scheduleService.updateUserSchedule(new Userschedule().setUserid(userschedule1.getUserid()).setTimeconfig(ssb.toString()).setScheduledate(nextThisDay));//更新下周该天的日程
            if (j > 0) {
                return "ok";
            }
        }
        return "false";
    }

    /*
     * 清空选则周的日程*/
    @ResponseBody
    @RequestMapping("/delete")
    public String deleteSchedule(String weekendScheduleform) {
        String[] all = weekendScheduleform.split(",");
        System.out.println("---------------------" + all[0]);
        Userschedule schedule2 = new Userschedule();
        int condition = 0;
        for (String s : all) {
            schedule2.setTimeconfig("0000000000");
            schedule2.setUserid(loginSysUser.getId());
            schedule2.setScheduledate(s);
            /*查询日程包含3则不执行重置*/
            String deleteTime = scheduleService.findUserSchedule(schedule2).getTimeconfig();
            int result = deleteTime.indexOf("3");
            if (result != -1) {
                continue;
            }
            condition = scheduleService.clearAllSchedule(schedule2);
        }
        if (condition > 0) {
            return "ok";
        }
        return "false";
    }

}
